﻿using System;
using System.Web;
using System.Web.Mvc;
using SenileCoderMvc.Library.DataAccess;
using SenileCoderMvc.Library.DataAccess.Queries;
using SenileCoderMvc.Library.Models;

namespace SenileCoderMvc.Controllers
{
    public class SnippetsController : Controller
    {
        private readonly ISupportsDataAccess dao;

        public SnippetsController(ISupportsDataAccess dao)
        {
            this.dao = dao;
        }

        //
        // GET: /Snippets/

        public ActionResult Index()
        {
            var query = new GetAllItems<CodeSnippet>();
            var snippets = dao.Query(query);

            return View(snippets);
        }

        //
        // GET: /Snippets/Details/5

        public ActionResult Details(int id)
        {
            var snippet = dao.GetById<CodeSnippet>(id);

            return View(snippet);
        }

        //
        // GET: /Snippets/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Snippets/Create

        [HttpPost, HandleError]
        public ActionResult Create(FormCollection collection)
        {
            var snippet = new CodeSnippet
                              {
                                  Name = HttpUtility.HtmlEncode(collection["Name"]),
                                  Snippet = HttpUtility.HtmlEncode(collection["Snippet"]),
                                  Created = DateTime.Now,
                              };
            dao.SaveOrUpdate(snippet);

            return RedirectToAction("Index");
        }

        //
        // GET: /Snippets/Edit/5

        public ActionResult Edit(int id)
        {
            var snippet = dao.GetById<CodeSnippet>(id);

            return View(snippet);
        }

        //
        // POST: /Snippets/Edit/5

        [HttpPost, HandleError]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var snippet = dao.GetById<CodeSnippet>(id);

            try
            {
                snippet.Name = HttpUtility.HtmlEncode(collection["Name"]);
                snippet.Snippet = HttpUtility.HtmlEncode(collection["Snippet"]);
                dao.SaveOrUpdate(snippet);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(snippet);
            }
        }

        //
        // GET: /Snippets/Delete/5

        public ActionResult Delete(int id)
        {
            var snippet = dao.GetById<CodeSnippet>(id);

            return View(snippet);
        }

        //
        // POST: /Snippets/Delete/5

        [HttpPost, HandleError]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var snippet = dao.GetById<CodeSnippet>(id);
            dao.DeleteEntity(snippet);

            return RedirectToAction("Index");
        }
    }
}
